﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Reborn_Server.App1AppHandler;
using App1AppHandler;

namespace Reborn_Server.App1AppHandler
{
    public class Spell_VampiricTouch : Buff
    {
        public Spell_VampiricTouch()
        {
            minutesLeft = 1;
        }

        public int baseHpAdd = 0;

        public int baseHpAddRank2 = 65;

        public bool runEffect(int damage)
        {
            if (TargetCreatureBase.HP < 1)
            {
                RemoveSpell(false, false);
                return false;
            }

            int hpToAdd = 0;

            int percentToAdd = 10;

            if (spellIcon.Name.IndexOf("Rank 2") > -1)
            {
                percentToAdd = percentToAdd + CastingCreatureLevel;
            }
            else
            {
                percentToAdd = percentToAdd + (CastingCreatureLevel / 2);
            }

            hpToAdd = (int)(damage * (percentToAdd / 100.00));

            bool doUpdate = false;
            if (TargetCreatureBase.HP < TargetCreatureBase.MaxHP)
            {
                TargetCreatureBase.HP = TargetCreatureBase.HP + hpToAdd + (CastingCreatureWis / 3);
                TargetCreatureBase.Update_Character();
                doUpdate = true;
            }
            TargetCreatureBase.SendMessage("You absorb health! (" + hpToAdd.ToString() + ")", "spelldefense");

            return doUpdate;
        }

        override public void Resume(CreatureBase targetCreatureBase, SpellIcon _spellIcon, int timeRemaining, int castLevel, int castCharacterID, int castInt, int castWis, int optionalValue, string xmlData)
        {
            minutesLeft = timeRemaining;
            base.Resume(targetCreatureBase, _spellIcon, timeRemaining, castLevel, castCharacterID, castInt, castWis, optionalValue, xmlData);

            foreach (Buff buff in TargetCreatureBase.buffManager.Buffs)
            {
                if (buff.spellIcon.Name == base.spellIcon.Name && buff.SpellID != SpellID)
                {
                    buff.RemoveSpell(false, false);
                }
            }

            spellTimer.Start();
        }

        public void Cast(CreatureBase myCreatureBase, int CastLevel, long CreatureGUID)
        {
            List<MapItem> MapItemsWithin12;
            if (base.InitializeCast(myCreatureBase, CastLevel, CreatureGUID, true, out MapItemsWithin12) == false)
                return;

            if (spellIcon.Name.IndexOf("Rank 2") > -1)
                minutesLeft = CastingCreatureWis / 15;

            foreach (MapItem mapitem in MapItemsWithin12)
            {
                if (Math.Abs(mapitem.MapX - TargetCreatureBase.MapX) <= 8 && Math.Abs(mapitem.MapY - TargetCreatureBase.MapY) <= 8)
                {
                    foreach (CreatureBase creaturebase in mapitem.CreaturesBases)
                    {
                        if (creaturebase is Character)
                        {
                            Character character = (Character)creaturebase;
                            character.PlaySound("BuffCast", TargetCreatureBase.MapX, TargetCreatureBase.MapY);
                        }
                    }
                }
            }

            TargetCreatureBase.buffManager.UpdateBuffs();
            spellTimer.Start();
        }
    }
}
